package com.coldlz.mmp.proxy;

import io.netty.buffer.ByteBuf;
import io.netty.handler.codec.http.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;

/*
 *  每个连接都会有这个对象
 */
public class RecordInterceptor {
    private static final Logger log = LoggerFactory.getLogger(RecordInterceptor.class);
    private static final Recorder defalutRecorder = new Recorder() {
        @Override
        public void record(HttpRecordRequest request, ProxyInfo info) {

        }

        @Override
        public void record(HttpRecordResponse response) {

        }
    };

    private final Recorder recorder;
    HttpRecordRequest requestRecord = new HttpRecordRequest();
    HttpRecordResponse responseRecord = new HttpRecordResponse();

    public RecordInterceptor() {
        this(defalutRecorder);
    }

    public RecordInterceptor(Recorder recorder) {
        if (recorder == null) {
            throw new ProxyException("recorder 不能为空");
        }
        this.recorder = recorder;
    }

    public void recordRequestHttpMessage(HttpMessage httpMessage, ProxyInfo info) {
        if (httpMessage instanceof HttpRequest r) {
            requestRecord.setUri(r.uri());
            requestRecord.setMethod(r.method());
            requestRecord.setHttpVersion(r.protocolVersion());
        } else {
            log.warn("类型异常" + httpMessage.getClass());
            return;
        }
        HttpHeaders headers = httpMessage.headers();
        requestRecord.setHttpHeaders(headers);
        for (String name : headers.names()) {
            requestRecord.addHeader(name, headers.getAll(name));
        }
        requestRecord.setContent(new ArrayList<>(128));
    }

    public void recordRequestHttpContent(HttpContent httpContent, ProxyInfo info) {
        ByteBuf content = httpContent.content();
        int len = content.readableBytes();
        if (len > 0) {
            byte[] bytes = new byte[len];
            content.readBytes(bytes);
            requestRecord.getContent().add(bytes);
        }
        if (httpContent instanceof LastHttpContent) {
            // 保存请求到数据库
            recorder.record(requestRecord, info);
            responseRecord.setDbTableId(requestRecord.getDbTableId());
        }
    }

    public void recordResponseHttpMessage(HttpMessage httpMessage, ProxyInfo info) {
        if (httpMessage instanceof HttpResponse r) {
            HttpResponseStatus status = r.status();
            HttpHeaders headers = r.headers();
            responseRecord.setStatus(status);
            responseRecord.setHttpHeaders(headers);
            for (String name : headers.names()) {
                responseRecord.addHeader(name, headers.getAll(name));
            }
            responseRecord.setContent(new ArrayList<>(128));
        } else {
            log.warn("类型异常" + httpMessage.getClass());
        }
    }

    public void recordResponseHttpContent(HttpContent httpContent, ProxyInfo info) {
        ByteBuf content = httpContent.content();
        int len = content.readableBytes();
        if (len > 0) {
            byte[] bytes = new byte[len];
            content.readBytes(bytes);
            responseRecord.getContent().add(bytes);
        }
        if (httpContent instanceof LastHttpContent) {
            recorder.record(responseRecord);
        }
    }

}
